var app = getApp();
var ux = require('../../../../lib/ux.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{},
    account:{},
    tongyi: 2,
    verifyCodeTime: '获取验证码',
    verifyCode: '',
    buttonDisable: false,
    mobile: '',
    code: '',
    action: "/api/memberUser/memberGetsAccount.do",
    codeUrl: '/api/member/getCodes.do',
    addWxUrl:'/api/delivery/getDelMoneyRecords.do',
    addUrl: '/api/memberUser/getMoneyRecords.do',
    submit: "{{submit}}"
  },
  money: function (e) {
    this.setData({
      money: e.detail.value
    })
  },
  codes: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var $this = this;
    ux.request(this.data.action, {}, function (protocol) {
      $this.setData({
        account: protocol.data.account
      });
    });
    this.setData({
      userInfo: wx.getStorageSync("userInfo")
    })
  },

  onForm: function (event) {
    if (this.data.submit) {
      var $this = this;
      var dataset = event.currentTarget.dataset;
      ux.request(this.data.submit, detail.value, function (protocol) {
        if (protocol.success) {
          if (dataset.url) {
            wx.redirectTo(dataset.url);
          }
        }
      }, true);
    }
  },

  onTap: function (event) {
    var dataset = event.currentTarget.dataset;
    var data = this.data;
    if (dataset.key && dataset.val) {
      data[dataset.key] = dataset.val;
    }
    this.setData(data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  verifyCodeEvent: function (e) {
    if (this.data.buttonDisable) return false;
    var that = this;
    var mobile = this.data.mobile;
    var regMobile = /^1\d{10}$/;
    if (!regMobile.test(this.account.contactNumber)) {
      this.toast("手机号有误");
      return false;
    }
    that.setData({
      buttonDisable: true
    })
    var c = 120;
    var intervalId = setInterval(function () {
      c = c - 1;
      that.setData({
        verifyCodeTime: c + '秒后重发',
        buttonDisable: true
      })
      if (c == 0) {
        clearInterval(intervalId);
        that.setData({
          verifyCodeTime: '获取验证码',
          buttonDisable: false
        })
      }
    }, 1000)
    var param = { mobile: this.account.contactNumber };
    var $this = this;
    ux.request(this.data.codeUrl, param, function (protocol) {
      wx.showToast({
        title: '请注意查收！'
      })
    }, true);
  },
  tongyis: function () {
    if (this.data.tongyi == 2) {
      this.setData({
        tongyi: 1
      })
    } else {
      this.setData({
        tongyi: 2
      })
    }
  },
  tijiao: function (e) {
    let that = this;
    let formId = e.detail.formId;
    if (!this.data.money) {
      this.toast("请输入金额");
      return false;
    }
    if (this.data.tongyi == 2) {
      this.toast("请阅读规则并同意");
      return false;
    }
    wx.showLoading({
      title: '处理中...',
    })
    var parmas = {
      money: this.data.money,
      gz: this.data.tongyi
    }
    var $this = this;
    ux.request(this.data.addWxUrl, parmas, function (protocol) {
      wx.hideLoading();
      $this.toast(protocol.data.msg)
      if (protocol.data.msg == "提交成功！等待审核") {
        setTimeout(function () {
          var pages = getCurrentPages();
          var currPage = pages[pages.length - 1]; //当前页面
          var prevPage = pages[pages.length - 2]; //上一个页面
          wx.navigateBack({
            delta: 1
          })
        }, 1500);
      }
    }, true);
  },
  toast: function (params) {
    if (typeof (params) == "string") {
      params = { content: params };
    }
    this.setData({
      toast: params.content
    });
    var THIS = this;
    setTimeout(function () {
      THIS.setData({
        toast: false
      });
      if (params.complete) {
        params.complete();
      }
    }, params.time || 3000);
  },
  lookgz: function () {
    wx.navigateTo({
      url: '/pages/protocol/psserver',
    })
  },
  looksy: function () {
    wx.navigateTo({
      url: '/pages/protocol/shouyi',
    })
  }
})
